//
//  SegmentView.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/8/30.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "SegmentView.h"
@interface SegmentView()

@property(nonatomic,strong)UIImageView * arrowImageView;

@property(nonatomic,strong)UIScrollView * contentScrollView;

@property(nonatomic,strong)UIView * underLine;

@end
@implementation SegmentView

-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.contentScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        self.contentScrollView.showsVerticalScrollIndicator = NO;
        self.contentScrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:self.contentScrollView];
        
        

        
        self.underLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.height-1, self.width, 0.5)];
        self.underLine.backgroundColor = HexColor(@"#EEEEEE");
        [self addSubview:self.underLine];
    }
    return self;
}


-(void)setArrowPosition:(UpArrowPosition)arrowPosition{
    _arrowPosition = arrowPosition;
    [self.arrowImageView removeFromSuperview];
    
}

-(UIImageView*)arrowImageView{
    if(_arrowImageView == nil){
        _arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        _arrowImageView.size = CGSizeMake(Category_width(12), Category_height(6));
    }
    return _arrowImageView;
}



-(void)setOptions:(NSArray *)options{
    _options = options;
    
    for (UIView * v in self.contentScrollView.subviews) {
        [v removeFromSuperview];
    }
    
    CGFloat width = self.width/4.0;
    CGFloat height = self.height;
    self.contentScrollView.contentSize =CGSizeMake(_options.count*width, 0);
    for (int i = 0; i<_options.count; i++) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:_options[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button setTitleColor:HexColor(@"#222222") forState:UIControlStateNormal];
        button.frame = CGRectMake(i*width, 0, width, height);
        button.tag = i;
        [button addTarget:self action:@selector(optionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        if(i!=_options.count-1){
            UIView * verticalView = [[UIView alloc] initWithFrame:CGRectMake(button.right, height*0.25, 0.5, height*0.5)];
            verticalView.backgroundColor = HexColor(@"#EEEEEE");
            [self.contentScrollView addSubview:verticalView];
        }
        [self.contentScrollView addSubview:button];
    }
}


-(void)optionButtonClick:(UIButton*)button{
    if([self.delegate respondsToSelector:@selector(didSelectedOptionIndex:segmentView:)]){
        [self.delegate didSelectedOptionIndex:button.tag segmentView:self];
    }
}
@end
